#include "key.h"

void key_init(void)
{
    RCC_KEY1_INIT();
    RCC_KEY2_INIT();
    
    GPIO_InitTypeDef led_init = {0};
    
    led_init.Pin = PIN_KEY1;
    led_init.Mode = GPIO_MODE_INPUT;
    led_init.Pull = GPIO_PULLUP;
    led_init.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIO_KEY1, &led_init);
    
    led_init.Pin = PIN_KEY2;
    HAL_GPIO_Init(GPIO_KEY2, &led_init);
}



uint8_t key_scan(uint8_t mode)
{
    uint8_t key_press = 0;
    uint8_t key_val = 0;
    
    if (mode) key_press = 0;
    
    if (!key_press && (!KEY1 || !KEY2))
    {
        key_press = 1;
        delay_ms(10);
        
        if (!KEY1) key_val = KEY1_PRESS;
        else if (!KEY2) key_val = KEY2_PRESS;
        else key_val = 0;
    }
    
    return key_val;
}


